<html>
<head><meta charset="utf-8"><title>future incompatibility lint on extension methods · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html">future incompatibility lint on extension methods</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224181463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224181463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224181463">(Jan 27 2021 at 13:40)</a>:</h4>
<p>I'm getting this error in my codebase as of <a href="https://github.com/rust-lang/rust/issues/81383">#81383</a>:</p>
<div class="codehilite"><pre><span></span><code>error: a method with this name may be added to the standard library in the future
   --&gt; src/parser.rs:652:28
    |
652 |         let f = unsafe { f.unwrap_unchecked() };
    |                            ^^^^^^^^^^^^^^^^
    |
    = note: `-D unstable-name-collisions` implied by `-D warnings`
    = warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior!
    = note: for more information, see issue #48919 &lt;https://github.com/rust-lang/rust/issues/48919&gt;
    = help: call with fully qualified syntax `util::OptionExt::unwrap_unchecked(...)` to keep using the current method
    = help: add `#![feature(option_result_unwrap_unchecked)]` to the crate attributes to enable `std::option::Option::&lt;T&gt;::unwrap_unchecked`
</code></pre></div>
<p>The thing I don't like about this error is that it doesn't give me any way to suppress the warning without changing the code. The reason for the error is because I have an extension method called <code>unwrap_unchecked</code> on option which does exactly what the recent PR does, and I want my code to break when it lands on stable. I can't set <code>#![feature(option_result_unwrap_unchecked)]</code> because I compile on stable. If I were to change the code to <code>OptionExt::unwrap_unchecked(f)</code> as suggested, besides looking uglier it also wouldn't remind me to clean it up when it lands on stable.</p>



<a name="224183096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224183096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224183096">(Jan 27 2021 at 13:52)</a>:</h4>
<p>What do other people do when faced with this situation? From what I can tell the actual future incompatibility mechanism is fairly general, and this is simply the first time I've run into it. But it does seem to be the kind of thing that would come up frequently, since methods that people care enough to make extension methods for are also likely to eventually end up in the standard library.</p>



<a name="224183157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224183157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224183157">(Jan 27 2021 at 13:53)</a>:</h4>
<p>The least bad current solution is to <code>allow(unstable_name_collisions)</code> somewhere "appropriate"; and that's precisely the issue with allowing it: the most convenient place is to do it a the crate root, at which point the lint serves no purpose whatsoever, so the best approach is to use it as close as possible to each "accepted call-site":</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[allow(unstable_name_collisions)]</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">f</span><span class="p">.</span><span class="n">unwrap_unchecked</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>Imho, there should be a way to express that a function / method is a polyfill for a future-provided stdlib method:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">OptionExt</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[polyfills( ::core::option::Option&lt;T&gt;::unwrap_unchecked )]</span><span class="w"> </span><span class="c1">// &lt;- bikeshed syntax here</span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">unwrap_unchecked</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="err">…</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<hr>
<p>EDIT: <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> actually, maybe <code>#[cfg(accessible(…))]</code> would help here?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg(not(accessible(Option::unwrap_unchecked)))]</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">OptionExt</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="err">…</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="224184107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224184107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224184107">(Jan 27 2021 at 14:00)</a>:</h4>
<p>Oh, I guess <code>cfg(accessible(...))</code> doesn't actually exist yet?</p>



<a name="224184780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224184780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224184780">(Jan 27 2021 at 14:05)</a>:</h4>
<p>Is there something like <code>#[cfg(rustc_version &lt; "1.51.0")]</code>?</p>



<a name="224185009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224185009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224185009">(Jan 27 2021 at 14:07)</a>:</h4>
<p>because then the extension method could be guarded behind that, or even</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg(rustc_version &gt;= </span><span class="s">"1.51.0"</span><span class="cp">)]</span><span class="w"></span>
<span class="fm">compile_error!</span><span class="p">(</span><span class="s">"note to self: delete OptionExt::unwrap_unchecked"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="224194782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224194782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224194782">(Jan 27 2021 at 15:12)</a>:</h4>
<p>Oh dang, the "least bad" solution doesn't work either - <code>#[allow(unstable_name_collisions)]</code> at block scope doesn't prevent the lint, it has to be at least function scope</p>



<a name="224382900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224382900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224382900">(Jan 28 2021 at 19:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods/near/224184780">said</a>:</p>
<blockquote>
<p>Is there something like <code>#[cfg(rustc_version &lt; "1.51.0")]</code>?</p>
</blockquote>
<p><a href="https://docs.rs/rustversion">https://docs.rs/rustversion</a></p>



<a name="224496071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224496071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224496071">(Jan 29 2021 at 16:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods/near/224194782">said</a>:</p>
<blockquote>
<p>Oh dang, the "least bad" solution doesn't work either - <code>#[allow(unstable_name_collisions)]</code> at block scope doesn't prevent the lint, it has to be at least function scope</p>
</blockquote>
<p>that seems like a bug, maybe open an issue?</p>



<a name="224508655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224508655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224508655">(Jan 29 2021 at 17:47)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span> <a href="https://github.com/rust-lang/rust/issues/81522">#81522</a></p>



<a name="224903010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/future%20incompatibility%20lint%20on%20extension%20methods/near/224903010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/future.20incompatibility.20lint.20on.20extension.20methods.html#224903010">(Feb 02 2021 at 17:11)</a>:</h4>
<p>FWIW, I would love to see accessible implemented, and it has some implementation challenges. I think it could use some extra eyes looking at it.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>